var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var devFlagConfig = {
	__DEV__: true,
	__DEVTOOLS__: true,
};
var plugins = [
	// 分离代码
	// extract inline css into separate 'style.css'
	new ExtractTextPlugin('[name].style.css', {
		disable: false,
		allChunks: true
	}),
	// 共享代码
	new webpack.optimize.CommonsChunkPlugin( /* chunkName= */ 'vendor', /* filename= */ 'vendor.js'),
	new webpack.HotModuleReplacementPlugin(),
	new webpack.NoErrorsPlugin(),
	new webpack.DefinePlugin(devFlagConfig),
	// new webpack.ProvidePlugin({
	// 	'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
	// }),
	// 根据模板插入css/js等生成最终HTML
	new HtmlWebpackPlugin({
		// title: '数据监控平台',
		filename: 'app.html',
		template: './templates/app.html',
		inject: true,
		hash: true, // 开启掉追加哈希的行为
		minify: {
			removeComments: false,
			collapseWhitespace: false
		}
	})
];

module.exports = {
	// configuration
	entry: {
		'app': [
			'eventsource-polyfill', // necessary for hot reloading with IE
			'webpack-hot-middleware/client',
			'./startup/app.jsx'
		],
		vendor: ['shim', 'sham', 'babel-polyfill', 'react']
	},
	output: {
		path: path.join(__dirname, 'assets/dist/dev'),
		filename: "[name].bundle.js",
		chunkFilename: "[id].bundle.js",
		publicPath: "/static/"
	},
	// devtool: "cheap-module-eval-source-map",
	devtool: "source-map",
	stats: {
		// Configure the console output
		colors: true,
		modules: true,
		reasons: true
	},
	module: {
		loaders: [
			// Less/css代码
			{
				test: /\.less|\.css$/,
				exclude: [
					path.resolve(__dirname, 'node_modules')
				],
				// modules 模块化，可以在样式中composes: className. 然后less已经解决了这个问题
				// minimize 最小化
				// sourceMap 源文件隐射
				loader: ExtractTextPlugin.extract("style-loader", "css-loader?modules&minimize&sourceMap!autoprefixer-loader!less-loader?sourceMap")
			}, {
				test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
				loader: 'url?limit=10000&minetype=application/font-woff'
			}, {
				test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
				loader: 'url?limit=10000&minetype=application/font-woff'
			}, {
				test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
				loader: 'url?limit=10000&minetype=application/octet-stream'
			}, {
				test: /\.ijmap(\?v=\d+\.\d+\.\d+)?$/,
				loader: 'url?limit=10000&minetype=application/font-woff'
			}, {
				test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
				loader: 'file'
			}, {
				test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
				loader: 'url?limit=10000&minetype=image/svg+xml'
			}, {
				test: /\.(jpe?g|png|gif|svg)$/i,
		        loaders: [
		            'file?hash=sha512&digest=hex&name=[hash].[ext]',
		            'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
		        ]
			}, {
				test: /\.js[x]?$/,
				include: __dirname,
				exclude: [
					path.resolve(__dirname, 'node_modules')
				],
				loader: 'babel'
			}
		]
	},
	progress: true,
	keepalive: true,
	resolve: {
		extensions: ['', '.js', '.jsx'],
		alias: {
			// 上线时要更换为'history/lib/createBrowserHistory'
			'historyFactory': 'history/lib/createBrowserHistory', //'history/lib/createHashHistory',
			'shim': 'es5-shim/es5-shim.min.js',
			'sham': 'es5-shim/es5-sham.min.js',
			// 'babel-polyfill': 'babel-polyfill/dist/polyfill.min.js',
			// avalon: path.resolve(path.join(__dirname, "./vendor/avalon.js"))
		}
	},
	plugins: plugins,
	watchOptions: {
		aggregateTimeout: 500, // default 300ms
		poll: true
	}
};